package com.ttbj.controller.order;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.company.CompanyService;
import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.SalesmanDto;
import com.ttbj.api.dto.order.OrderCustomDriverDto;
import com.ttbj.api.dto.order.OrderCustomDto;
import com.ttbj.api.dto.order.OrderWorkDto;
import com.ttbj.api.dto.order.WaybillDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.order.OrderService;
import com.ttbj.constant.Const;
import com.ttbj.constant.annotation.SystemControllerLog;
import com.ttbj.core.util.JsonTools;
import com.ttbj.util.ViewDataUtil;


@Controller
public class OrderController {
	
	
	@Autowired
	private OrderService orderService;
	@Autowired
	private CompanyService companyService;
	
	
	/**
	 * 支付成功账单页面
	 * @return
	 */
	@RequestMapping(value="/order/suc_order_list.htm")
	public String suc_order_list() {
		return "order/suc_order_list";
	}
	
	/**
	 * 支付成功账单详情页面
	 * @return
	 */
	@RequestMapping(value="/order/suc_details_order.htm")
	public String suc_details_order() {
		return "order/suc_details_order";
	}
	
	/**
	 * 搬家预订单页面
	 * @return
	 */
	@RequestMapping(value="/order/fail_order_list.htm")
	public String fail_order_list() {
		return "order/fail_order_list";
	}
	
	/**
	 * 搬家预订单详情页面
	 * @return
	 */
	@RequestMapping(value="/order/fail_details_order.htm")
	public String fail_details_order() {
		return "order/fail_details_order";
	}
	
	/**
	 * 预订单分配业务员页面
	 * @return
	 */
	@RequestMapping(value="/order/distribute_salesman.htm")
	public String distribute_salesman() {
		return "order/distribute_salesman";
	}
	
	/**
	 * 预订单分配司机页面
	 * @return
	 */
	@RequestMapping(value="/order/distribute_driver.htm")
	public String distribute_driver() {
		return "order/distribute_driver";
	}
	
	/**
	 * 分页条件查询预订单信息，查询预约状态和搬运单分配给业务员
	 * @param request 
	 * @param orderCusomDto 封装了条件查询的信息
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/order/getAdvanceSalOrderInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getAdvanceSalOrderInfoByPage(HttpServletRequest request, OrderCustomDto orderCustomDto, Page page) {
		try {
			List<OrderCustomDto> orderCustomDtos = this.orderService.findAdvanceSalOrderInfoByPage(orderCustomDto, page);
			int count = 0;
			if (orderCustomDtos !=null) {
				nickNameParseString(orderCustomDtos);
				count = this.orderService.findAdvanceSalOrderInfoByPageCount(orderCustomDto);
			}
			return ViewDataUtil.buildSuccessPageJson("分配给业务员订单列表", orderCustomDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	
	/**
	 * 分页查询预订单信息，只查状态转为手动的搬运单分配司机
	 * @param request 
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/order/getAdvanceDriOrderInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getAdvanceDriOrderInfoByPage(HttpServletRequest request, Page page) {
		try {
			List<OrderCustomDriverDto> orderCustomDriverDtos = this.orderService.findqueryAdvanceDriOrderInfoByPage(page);
			int count = 0;
			if (orderCustomDriverDtos !=null) {
				nickNameParseString2(orderCustomDriverDtos);
				count = this.orderService.findAdvanceDriOrderInfoByPageCount();
			}
			return ViewDataUtil.buildSuccessPageJson("分配给司机订单列表", orderCustomDriverDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	
	/**
	 * 分页条件查询支付及账单信息，只查未支付的
	 * @param request 
	 * @param orderCusomDto 封装了条件查询的信息
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/order/getAdvanceOrderInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getAdvanceOrderInfoByPage(HttpServletRequest request, OrderCustomDto orderCustomDto, Page page) {
		//获取当前登录的用户信息
		SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
		//通过公司编号获取登录人的公司id
		Long comnumId = this.companyService.findCompanyIdByNum(salesmanDto.getSalesmanComnum());
		//设置该登录用户的公司id
		orderCustomDto.setComnumId(comnumId);
		try {
			List<OrderCustomDto> orderCustomDtos = this.orderService.findAdvanceOrderInfoByPage(orderCustomDto, page);
			int count = 0;
			if (orderCustomDtos != null) {
				nickNameParseString(orderCustomDtos);
				count = this.orderService.findAdvanceOrderInfoByPageCount(orderCustomDto);
			}
			return ViewDataUtil.buildSuccessPageJson("支付及账单列表", orderCustomDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	/**
	 * 分页条件查询支付及账单信息，只查状态是支付成功的
	 * @param request 
	 * @param orderCusomDto 封装了条件查询的信息
	 * @param page 分页信息
	 * @return
	 */
	@RequestMapping(value="/order/getCompleteOrderInfoByPage.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	public String getCompleteOrderInfoByPage(HttpServletRequest request, OrderCustomDto orderCustomDto, Page page) {
		System.out.println(orderCustomDto.getOrderIsMove());
		//获取当前登录的用户信息
		SalesmanDto salesmanDto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
		//通过公司编号获取登录人的公司id
		Long comnumId = this.companyService.findCompanyIdByNum(salesmanDto.getSalesmanComnum());
		//设置该登录用户的公司id
		orderCustomDto.setComnumId(comnumId);
		try {
			List<OrderCustomDto> orderCustomDtos = this.orderService.findCompleteOrderInfoByPage(orderCustomDto, page);
			int count = 0;
			if (orderCustomDtos != null) {
				nickNameParseString(orderCustomDtos);
				count = this.orderService.findCompleteOrderInfoByPageCount(orderCustomDto);
			}
			return ViewDataUtil.buildSuccessPageJson("支付及账单列表", orderCustomDtos, count);
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	//把二进制数组转成string字符串，用户显示出微信用户的昵称
	private void nickNameParseString(List<OrderCustomDto> orderCustomDtos) {
		//把微信用户的昵称信息转成String类型
		for (int i = 0; i < orderCustomDtos.size(); i++) {
			byte[] bs = orderCustomDtos.get(i).getNickName();
			try {
				orderCustomDtos.get(i).setViewNickName(new String(bs, "utf-8"));
				//把byte数组字节的昵称设置成null，gson不输出
				orderCustomDtos.get(i).setNickName(null);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}
	
	//把二进制数组转成string字符串，用户显示出微信用户的昵称
	private void nickNameParseString2(List<OrderCustomDriverDto> orderCustomDriverDtos) {
		//把微信用户的昵称信息转成String类型
		for (int i = 0; i < orderCustomDriverDtos.size(); i++) {
			byte[] bs = orderCustomDriverDtos.get(i).getNickName();
			try {
				orderCustomDriverDtos.get(i).setViewNickName(new String(bs, "utf-8"));
				//把byte数组字节的昵称设置成null，gson不输出
				orderCustomDriverDtos.get(i).setNickName(null);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 增加预订单与业务员之间的信息，完成预订单分配给业务员
	 * @param request
	 * @param orderWorkDto
	 * @return
	 */
	@RequestMapping(value="/order/addedOrderWorkInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="订单分配业务员")
	public String addedOrderWorkInfo(OrderWorkDto orderWorkDto) {
		try {
			this.orderService.addToOrderWorkInfo(orderWorkDto);
			return ViewDataUtil.buildSuccessJson("分配成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
	
	
	/**
	 * 增加预订单与司机之间的信息，完成预订单分配给司机
	 * @param request
	 * @param orderWorkDto
	 * @return
	 */
	@RequestMapping(value="/order/addedOrderWaybillInfo.htm", method=RequestMethod.POST, produces={"text/html;charset=UTF-8;","application/json;"})
	@ResponseBody
	@SystemControllerLog(description="订单分配司机")
	public String addedOrderWaybillInfo(@RequestBody List<WaybillDto> waybillDtos) {
		System.out.println(JsonTools.GsonString(waybillDtos));
		try {
			this.orderService.addToWaybillInfo(waybillDtos);
			return ViewDataUtil.buildSuccessJson("分配成功");
		} catch (BusinessException e) {
			return ViewDataUtil.buildFailureJson(e.getErrorCode(), e.getMessage());
		}
	}
}
